﻿using M_Purchase.Api.Appliction.Command.AllocateCommand;
using M_Purchase.Domain.DTO;
using M_Purchase.Domain;
using MediatR;
using M_Purchase.Repository.PurchaseInterface;

namespace M_Purchase.Api.Appliction.CommandHandlers.AllocateCommandHandler
{
    /// <summary>
    /// 显示调拨命令处理器
    /// </summary>
    public class ShowAllocateCommandHandler : IRequestHandler<ShowAllocateCommand, AllocateDto<Allocate>>
    {
        /// <summary>
        /// 调拨仓储接口
        /// </summary>
        protected readonly IAllocateRepository allocateRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="allocateRepository">调拨仓储接口</param>
        public ShowAllocateCommandHandler(IAllocateRepository allocateRepository)
        {
            this.allocateRepository = allocateRepository;
        }

        /// <summary>
        /// 处理显示调拨命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<AllocateDto<Allocate>> Handle(ShowAllocateCommand request, CancellationToken cancellationToken)
        {
            var list = from a in await allocateRepository.GetAllAsync()
                       where !a.IsDeleted
                       select new Allocate()
                       {
                           AllocateId=a.AllocateId,
                           AllocateNumber=a.AllocateNumber,
                           AllocateDate=a.AllocateDate,
                           AllocateNum=a.AllocateNum,
                           AllocatePerson=a.AllocatePerson,
                           AllocateReason=a.AllocateReason,
                           Notes=a.Notes,
                           Associate=a.Associate,
                           Transport=a.Transport,
                           ApprovedDate=a.ApprovedDate,
                           Reviewer=a.Reviewer,
                           AllocateStatus=a.AllocateStatus,
                           CreatedBy = a.CreatedBy,
                           CreatedDate = a.CreatedDate,
                           UpdatedBy = a.UpdatedBy,
                           UpdatedDate = a.UpdatedDate,
                           IsDeleted = a.IsDeleted
                       };
            if (!string.IsNullOrEmpty(request.AllocateNumber))
            {
                list = list.Where(x => x.AllocateNumber.Contains(request.AllocateNumber));
            }

            if (request.AllocateDate != null)
            {
                list = list.Where(x => x.AllocateDate == request.AllocateDate);
            }

            if (!string.IsNullOrEmpty(request.AllocateStatus))
            {
                list = list.Where(x => x.AllocateStatus.Contains(request.AllocateStatus));
            }

            if (request.PageIndex == null)
            {
                request.PageIndex = 1;
            }

            if (request.PageSize == null)
            {
                request.PageSize = 15;
            }

            AllocateDto<Allocate> page = new AllocateDto<Allocate>();
            page.TotalCount = list.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / (int)request.PageSize);
            page.list = list.OrderByDescending(x => x.CreatedDate).Skip((int)request.PageIndex - 1).Take((int)request.PageSize).ToList();
            return page;
        }
    }
}
